package com.sanpu.kxe.business.trade.presenter;

import com.sanpu.kxe.business.trade.bean.MyRebTotalOutput;
import com.sanpu.kxe.business.trade.contract.RebTotalContract;
import com.sanpu.kxe.business.trade.model.TradeModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：我的返佣详情
 * Created by 薛勇军 on 2018/8/8.
 */

public class RebTotalPresenter implements RebTotalContract.Presenter {

    private RebTotalContract.View mView;

    private CompositeSubscription mSubscription;

    private String mRebLvl;

    public RebTotalPresenter(RebTotalContract.View mView, String mRebLvl) {
        this.mView = mView;
        this.mRebLvl = mRebLvl;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMyRebTotalList(final int nowPage) {
        Subscription subscription = TradeModel.getInstance().myHstRebateList(nowPage, mRebLvl)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MyRebTotalOutput>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<MyRebTotalOutput> myRebTotalOutputs) {
                        if (myRebTotalOutputs != null) {
                            mView.showRebTotalListView(myRebTotalOutputs, nowPage == 0);
                            if (myRebTotalOutputs.size() < CommonConfig.PAGENUMER) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
